﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;
using Microsoft.SharePoint.Taxonomy;

namespace VH.SharePoint.Api.Entensions
{
    public static class SPListItemExtensions
    {
        public static T ToBlogItem<T>(this SPListItem item) where T : BlogItem
        {
            var tType = typeof (T);
            var ctor=tType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] {typeof (SPListItem)},null);
            return ctor.Invoke(new object[]{ item }) as T;
        }
        public static void SetFieldValue<T>(this SPListItem item, T value, Guid fieldId)
        {
            item[fieldId] = value;
        }
        public static void SetFieldValue<T>(this SPField field, SPListItem item, T value)
        {
            item.SetFieldValue(value,field.Id);
        }
        public static void SetFieldValue<T>(this SPListItem item, string fieldName, T value)
        {
            item.Fields.GetField(fieldName).SetFieldValue(item, value);
        }
        public static T GetFieldValue<T>(this SPListItem item, Guid fieldId)
        {
            try
            {
                SPField fieldByInternalName = item.Fields[fieldId];
                object obj = item[fieldByInternalName.Id];
                if (obj is string)
                {
                    try
                    {
                        if (fieldByInternalName.Type == SPFieldType.Guid)
                            obj = (object)new Guid((string)obj);
                        else if (fieldByInternalName.Type != SPFieldType.Text)
                        {
                            if (fieldByInternalName.Type != SPFieldType.Note)
                            {
                                if (!IsLookupToDocsField(fieldByInternalName.InternalName))
                                    obj = fieldByInternalName.GetFieldValue((string)obj);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                if (fieldByInternalName.Type == SPFieldType.DateTime && obj is DateTime)
                    obj = (object)DateTime.SpecifyKind((DateTime)obj, DateTimeKind.Utc);
                if (fieldByInternalName is TaxonomyField)
                {
                    if (obj is TaxonomyFieldValue && typeof(T) == typeof(TaxonomyFieldValueCollection))
                        obj = (object)new TaxonomyFieldValueCollection(fieldByInternalName) { (TaxonomyFieldValue)obj };
                    if (obj is TaxonomyFieldValueCollection && typeof(T) == typeof(TaxonomyFieldValue))
                    {
                        var obj2 = ((TaxonomyFieldValueCollection)obj);

                        obj = (object)(obj2.Count > 0 ? obj2[0] : default(TaxonomyFieldValue));

                    }

                }
                return (T)obj;
            }
            catch (Exception)
            {

            }
            return default(T);

        }
        static bool IsLookupToDocsField(string strInternalName)
        {
            return strInternalName == "FileRef" || strInternalName == "FileDirRef" || (strInternalName == "FileLeafRef" || strInternalName == "ClientId") || (strInternalName == "Last_x0020_Modified" || strInternalName == "Created_x0020_Date" || (strInternalName == "File_x0020_Size" || strInternalName == "FSObjType")) || (strInternalName == "UniqueId" || strInternalName == "MetaInfo" || (strInternalName == "ProgId" || strInternalName == "ScopeId") || (strInternalName == "IsCheckedoutToLocal" || strInternalName == "DocConcurrencyNumber" || (strInternalName == "ItemChildCount" || strInternalName == "FolderChildCount")));
        }
    }
}
